<?php
class BuyAction extends FrontendAction
{
    public function Index()
    {
        $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : $this->error('参数错误');
        $item = D('Item')->where('id='.$id)->find();
        if (!$item) {
            $this->error('商品不存在');
            redirect(__APP__);
        }
        $item['amount'] = $item['sprice_total'] = $item['sprice'];

        if ($_POST) {
            $user_info = $this->check_login();
            $order_mod = D('Order');
            if( false === $vo = $order_mod->create() ){
	            $this->error( $order_mod->error() );
	        }
            if ($order_mod->number < 1) {
                $this->error('购买数量不能小于1份');
                redirect(u('Buy/Index', array('id'=>$item['id'])));
            }
            $order_mod->uid = $user_info['uid'];
            $order_mod->item_id = $id;
            $order_mod->create_time = time();
            $order_id = $order_mod->add();
            redirect(u('Buy/check', array('order_id'=>$order_id)));
        }

        $this->assign('item', $item);
        $this->display();
    }

    public function check()
    {
        $user_info = $this->check_login();
        $order_id = isset($_GET['order_id']) && intval($_GET['order_id']) ? intval($_GET['order_id']) : $this->error('参数错误');
        $order_mod = D('Order');
        $order_info = $order_mod->where('id='.$order_id)->find();
        if (!$order_info) {
            $this->error('订单不存在');
        }
        if ($order_info['uid'] != $user_info['uid']) {
            redirect(u('Goods/view', array('id'=>$order_info['item_id'])));
        }

        $this->assign('order_info', $order_info);
        $this->display();
    }

    public function payment()
    {
        $user_info = $this->check_login();
        $order_id = isset($_POST['order_id']) && intval($_POST['order_id']) ? intval($_POST['order_id']) : $this->error('参数错误');
        $order_mod = D('Order');
        $order_info = $order_mod->where('id='.$order_id)->find();
        if (!$order_info) {
            $this->error('订单不存在');
        }
        if ($order_info['uid'] != $user_info['uid']) {
            redirect(u('Goods/view', array('id'=>$order_info['item_id'])));
        }
        if ($_POST['payment_type'] == 'alipay') {
            include( APP_PATH . '/../payment/alipay/alipay.php' );

            $payment_config = array(
                'alipay_service' => 'create_direct_pay_by_user',
                'alipay_partner' => '2088002337270972',
                'seller_email' => 'shenqin@live.com',
                'security_code' => 'u0ky4v38f1s8fyyccyg82m29ben2g7ov',
                'sign_type' => 'MD5',
                'notify_url' => u('payment/pay_notify'),
                'return_url' => u('payment/pay_return'),
            );
            $payment_obj = new AlipayPayment($payment_config);
            $payment_order_info = array(
                'orderid' => $order_info['id'],
                'subject' => '果趣订单：' . $order_info['id'],
                'body' => '果趣订单：' . $order_info['id'],
                'price' => $order_info['amount'],
                'quantity' => $order_info['quantity'],
                'shipping_fee' => '0',
            );
            $url = $payment_obj->create_url($payment_order_info);
            redirect($url);
        } else {
            $this->error('无效的支付方式！');
        }
    }
}
?>